package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public Result submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return Result.success();
    }


    @GetMapping("/userPage")
    public Result<Page<OrdersDto>> userPage(Integer page, Integer pageSize) {

        Page<Orders> ordersPage = new Page<>();
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据登录用户id查询订单信息
        ordersLambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        List<Orders> list = ordersService.list(ordersLambdaQueryWrapper);

        Page<OrdersDto> ordersDtoPage = new Page<>();
        ordersDtoPage.setTotal(ordersPage.getTotal());

        //把订单的信息封装到OrdersDto中
        List<OrdersDto> ordersDtoList = list.stream().map(orders ->
                {

                    OrdersDto ordersDto = new OrdersDto();
                    BeanUtils.copyProperties(orders, ordersDto);


                    //把订单明细信息封装到OrdersDto中,根据orderId查询对应的订单明细
                    LambdaQueryWrapper<OrderDetail> orderDetailWrapper = new LambdaQueryWrapper<>();
                    orderDetailWrapper.eq(OrderDetail::getOrderId, orders.getId());
                    List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailWrapper);
                    ordersDto.setOrderDetails(orderDetailList);

                    return ordersDto;
                }
        ).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtoList);

        return Result.success(ordersDtoPage);



    }


    @GetMapping("/page")
    public Result<Page<Orders>> page(Integer page,
                       Integer pageSize,
                       String number,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<Orders> orderPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(number), Orders::getNumber, number);
        wrapper.between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);
        wrapper.orderByDesc(Orders::getOrderTime);

        ordersService.page(orderPage, wrapper);

        return Result.success(orderPage);

    }


    @PutMapping
    public Result updateStatus(@RequestBody Orders orders){

        LambdaUpdateWrapper<Orders> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Orders::getId, orders.getId());
        wrapper.set(Orders::getStatus, orders.getStatus());

        ordersService.update(wrapper);

        return Result.success();
    }
}
